package cn.edu.pku.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.github.pagehelper.PageInfo;

import cn.edu.pku.constant.EnumStatus;
import cn.edu.pku.entity.Borrower;
import cn.edu.pku.service.BorrowerService;
import cn.edu.pku.vo.BorrowInfoResult;
import cn.edu.pku.vo.BorrowerResult;
import cn.edu.pku.vo.request.BorrowInfoRequest;
import cn.edu.pku.vo.request.BorrowerRequest;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;

@RestController
@Slf4j
public class BorrowerController {

	@Autowired
	BorrowerService borrowerService;
	
	@RequestMapping(value="getborrowerlist",method={RequestMethod.GET})
	@ApiOperation(value="分页获取借阅人数据接口", notes="传入pageNow,pageSize,borrowerName")
	public BorrowerResult getBorrowerList(Integer pageNow,Integer pageSize,String borrowerName){
		log.info("分页获取借阅人数据接口");
		BorrowerResult br=new BorrowerResult();
		PageInfo<Borrower> borrowerList = borrowerService.getBorrowerList(pageNow,pageSize,borrowerName);
		br.setData(borrowerList);
		return br;
	}

	@RequestMapping(value="saveborrower",method={RequestMethod.POST})
	@ApiOperation(value="保存借阅人数据接口", notes="传入借阅人的基本信息")
	public BorrowerResult saveBorrower(BorrowerRequest borrower){
		log.info("保存借阅人数据接口");
		BorrowerResult br=new BorrowerResult();
		Boolean saveBorrower = borrowerService.saveBorrower(borrower);
		if (saveBorrower) {
			br.setStatus(EnumStatus.SUCCESS.getStatus());
			br.setMsg(EnumStatus.SUCCESS.getDesc());
		} else {
			br.setStatus(EnumStatus.FAILED.getStatus());
			br.setMsg(EnumStatus.FAILED.getDesc());
		}
		return br;
	}
	@RequestMapping(value="updateborrower",method={RequestMethod.POST})
	@ApiOperation(value="修改借阅人数据接口", notes="修改借阅人的基本信息")
	public BorrowerResult updateBorrower(Borrower borrower){
		log.info("修改借阅人数据接口");
		BorrowerResult br=new BorrowerResult();
		Boolean saveBorrower = borrowerService.update(borrower);
		if (saveBorrower) {
			br.setStatus(EnumStatus.SUCCESS.getStatus());
			br.setMsg(EnumStatus.SUCCESS.getDesc());
		} else {
			br.setStatus(EnumStatus.FAILED.getStatus());
			br.setMsg(EnumStatus.FAILED.getDesc());
		}
		return br;
	}
	@RequestMapping(value="deleteborrower",method={RequestMethod.POST})
	@ApiOperation(value="删除借阅人数据接口", notes="删除借阅人的基本信息")
	public BorrowerResult deleteBorrower(Integer id){
		log.info("删除借阅人数据接口");
		BorrowerResult br=new BorrowerResult();
		Boolean saveBorrower = borrowerService.delBorrower(id);
		if (saveBorrower) {
			br.setStatus(EnumStatus.SUCCESS.getStatus());
			br.setMsg(EnumStatus.SUCCESS.getDesc());
		} else {
			br.setStatus(EnumStatus.FAILED.getStatus());
			br.setMsg(EnumStatus.FAILED.getDesc());
		}
		return br;
	}
	
	@RequestMapping(value="getborrowerbyid",method={RequestMethod.POST})
	@ApiOperation(value="根据主键获取借阅人数据接口", notes="根据主键id获取借阅人的基本信息")
	public BorrowerResult getBorrowerById(Integer id){
		BorrowerResult br=new BorrowerResult();
		Borrower borrowerById = borrowerService.getBorrowerById(id);
		if (borrowerById!=null) {
			br.setData(borrowerById);
			br.setStatus(EnumStatus.SUCCESS.getStatus());
			br.setMsg(EnumStatus.SUCCESS.getDesc());
		} else {
			br.setData(borrowerById);
			br.setStatus(EnumStatus.FAILED.getStatus());
			br.setMsg(EnumStatus.FAILED.getDesc());
		}
		return br;
	}
	
	
	
}
